<?php
/**
 * Created by PhpStorm.
 * User: luxuewen_gz
 * Date: 2019/1/3
 * Time: 10:23
 */

namespace app\admin\logic;


use think\Session;

class Index
{
    public function getMenu(){
        $userInfo = (new Session())->get('USER_INFO');
        $urWhere[] = ['is_delete', 'eq', 0];
        $urWhere[] = ['user_id', 'eq', $userInfo['userId']];
        $roleIds = array_unique(array_column(model('UserRole')->where($urWhere)->field('role_id')->select()->toArray(), 'role_id'));
        $rpWhere[] = ['is_delete', 'eq', 0];
        $rpWhere[] = ['role_id', 'in', $roleIds];
        $permissionIds = array_unique(array_column(model('RolePermission')->where($rpWhere)->field('permission_id')->select()->toArray(), 'permission_id'));
        $pmWhere[] = ['is_delete', 'eq', 0];
        $pmWhere[] = ['permission_id', 'in', $permissionIds];
        $menuIds = array_unique(array_column(model('PermissionMenu')->where($pmWhere)->field('menu_id')->select()->toArray(), 'menu_id'));
        $mWhere[] = ['is_delete', 'eq', 0];
        $mWhere[] = ['id', 'in', $menuIds];
        $menu = model('Menu')->where($mWhere)->where('pid', 'eq', 0)->field('id,menu_name,menu_icon')->select();
        foreach ($menu as $k => $v) {
            $secondMenu = model('Menu')->where($mWhere)->where('pid', 'eq', $v['id'])->field('menu_name,controller,method')->select();
            $menu[$k]['second'] = $secondMenu;
        }
        return $menu;
    }

}